home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacUserProj / MacUser Projects / June / 2GenApp Src / WindowUtil.c < prev   
Encoding:
C/C++ Source or Header  |  1990-04-25  |  12.9 KB  |  451 lines  |  [TEXT/KAHL]

  1. /* *****************************************************************************
  2.     FILE:             WindowUtil.c
  3.     
  4.     DESCRIPTION:     Window management utilities
  5.  
  6.     AUTHOR:            Kurt W.G. Matthies
  7.         
  8.     Copyright © 1990 by Code of the West, Inc., All Rights Reserved.
  9.     
  10.     Revision History:
  11.     ==========================================================
  12.     4.24.90    -    June 1990 Release: Revised for multiple window support
  13.     3.30.90    -    May 1990 MacUser Release
  14.     ==========================================================
  15.  
  16.    ***************************************************************************** */
  17. #include "AppGlobals.h"
  18.  
  19. #include "WindowUtilPr.h"
  20.  
  21. /* -----------------------------  Local Constants  ------------------------------ */
  22. #define    kWindowOffset    12        /* for each new window */
  23.  
  24.  
  25. /* ------------------  Local Prototypes  ---------------------------- */
  26.  
  27. void        moveScrollBars        ( WindowPtr );
  28. void        setPortClip            ( WindowPtr );
  29.  
  30.  
  31. /*---------------------------------------------------------------------------
  32.     calcNewWinRect -    create a window Rect for a new document, based on 
  33.     4.24.90kwgm            current top window size
  34. ----------------------------------------------------------------------------- */
  35. Rect *
  36. calcNewWinRect (topWin, newWinRectPtr)
  37.     WindowPtr        topWin;
  38.     register Rect    *newWinRectPtr;
  39. {
  40.     GrafPtr            savePort;
  41.     short            winHeight, winWidth, defWinHeight, defWinWidth;
  42.  
  43.     /* get top window rect */
  44.     GetPort (&savePort);            
  45.     *newWinRectPtr = topWin->portRect;
  46.     
  47.     SetPort (topWin);
  48.     
  49.     /* convert to desktop (global) space */
  50.     LocalToGlobal (&(topLeft(*newWinRectPtr)));
  51.     LocalToGlobal (&(botRight(*newWinRectPtr)));
  52.     
  53.     /* get window size */
  54.     winWidth = newWinRectPtr->right - newWinRectPtr->left;
  55.     winHeight = newWinRectPtr->bottom - newWinRectPtr->top;
  56.     
  57.     /* get screen size */
  58.     defWinWidth = gWindowRect.right - gWindowRect.left;
  59.     defWinHeight = gWindowRect.bottom - gWindowRect.top;
  60.     
  61.     /* if top window is an unusually large or small window, use gWindowRect */
  62.     if (winWidth < (defWinWidth - 50) || winHeight < (defWinHeight - 50) ||
  63.         newWinRectPtr->left < (gWindowRect.left - 20) ||
  64.         newWinRectPtr->bottom < (gWindowRect.bottom - 30))
  65.         *newWinRectPtr = gWindowRect;
  66.     
  67.     /* offset the rect by our stacking amount */
  68.     OffsetRect (newWinRectPtr, kWindowOffset, kWindowOffset);
  69.     
  70.     /* keep the window within the main screen */
  71.     newWinRectPtr->right = MIN(gScreenRect.right, newWinRectPtr->right);
  72.     newWinRectPtr->bottom = MIN(gScreenRect.bottom, newWinRectPtr->bottom);
  73.         
  74.     SetPort (savePort);
  75.  
  76.     return (newWinRectPtr);
  77.     
  78. } /* calcNewWinRect */
  79.  
  80. /* ------------------------------------------------------------------------------
  81.     doGrowWindow -    resize the window by tracking user input
  82.     4.24.90kwgm
  83. --------------------------------------------------------------------------------*/
  84. void 
  85. doGrowWindow (theWindow, aPt)
  86.     WindowPtr        theWindow;
  87.     Point            aPt;
  88. {
  89.     long            newSize;
  90.     Rect            limitRect;
  91.     
  92.     invalScrollBars (theWindow, true);    /* erase and invalidate old scroll bar area */
  93.     
  94.     /* maximum size of window to all of screen area (GrayRgn) */
  95.     limitRect = gGrayRgnRect;
  96.     limitRect.left = 48;    /* set so that minimum window's size is 48 by 48 */
  97.     limitRect.top = 48;
  98.     
  99.     /* track user mouse and grow the window */
  100.     newSize = GrowWindow (theWindow, aPt, &limitRect);
  101.     SizeWindow (theWindow, LoWord (newSize), HiWord (newSize), true);
  102.         
  103.     moveScrollBars (theWindow);        /* relocate scroll bars to new window bounds */
  104.     
  105.     invalScrollBars (theWindow, true);    /* erase and invalidate new scroll bar area */
  106.  
  107. } /* doGrowWindow */
  108.  
  109. /* ------------------------------------------------------------------------------
  110.     doZoomBox -        zoom the window after mouse in zoom-box
  111.     4.24.90kwgm
  112. --------------------------------------------------------------------------------- */
  113. void
  114. doZoomBox (theWindow, partCode)
  115.     WindowPtr        theWindow;
  116.     short            partCode;
  117. {
  118.     GrafPtr            savePort;
  119.     
  120.     GetPort (&savePort);
  121.     SetPort (theWindow);
  122.  
  123.     ZoomWindow (theWindow, partCode, false);    /* zoom the window */
  124.  
  125.     moveScrollBars (theWindow);        /* move to new window edges */
  126.  
  127.     InvalRect (&theWindow->portRect);    /* setup for redraw */
  128.  
  129.     SetPort (savePort);
  130.     
  131. } /* doZoomBox */
  132.  
  133. /* ----------------------------------------------------------------------
  134.     clickZoomWindow    -    zoom or un-zoom the window
  135.     4.24.90kwgm            called when there is a dbl-click in title bar
  136. ------------------------------------------------------------------------- */
  137. void
  138. clickZoomWindow (theWindow)
  139.     WindowPtr        theWindow;
  140. {
  141.     long              newSize;
  142.     WStateData        **wDataHdl;
  143.     Rect            userState, portRect;
  144.  
  145.     /* See IM-IV for WStateData description */
  146.     wDataHdl = ((WindowPeek)theWindow)->dataHandle;
  147.     userState = (*wDataHdl)->userState;
  148.     portRect = theWindow->portRect;
  149.     
  150.     /* convert to window manager port coordinates */
  151.     LocalToGlobal (&(topLeft(portRect)));
  152.     LocalToGlobal (&(botRight(portRect)));
  153.     
  154.     /* if zoomed, then un-zoom , else zoom */
  155.     if (EqualRect (&userState, &portRect))
  156.         doZoomBox (theWindow, inZoomOut);
  157.     else
  158.         doZoomBox (theWindow, inZoomIn);
  159.  
  160. } /* clickZoomWindow */
  161.  
  162. /* ------------------------------------------------------------------------------------
  163.     drawScrollBars -    draw the document's scroll bars
  164.     4.24.90kwgm
  165. --------------------------------------------------------------------------------------- */
  166. void
  167. drawScrollBars (theDoc, activate)
  168.     DocPtr            theDoc;
  169.     Boolean            activate;
  170. {
  171.     ControlHandle    theControl;
  172.     long            ref;
  173.     short            value;
  174.     Point            curScroll, maxScroll, docExtent, frameSize;
  175.     Rect            frameRect;
  176.     
  177.     setPortClip (theDoc);        /* clip out to port */
  178.     DrawGrowIcon (theDoc);
  179.     
  180.     theControl = ((WindowPeek)theDoc)->controlList;
  181.     
  182.     if ((theDoc == FrontWindow ()) && activate)
  183.     {
  184.         /* get frame size */
  185.         makeFrameRect (theDoc, &frameRect);
  186.         frameSize.h = frameRect.right - frameRect.left;
  187.         frameSize.v = frameRect.bottom - frameRect.top;
  188.         
  189.         /* use temp variables */
  190.         maxScroll = theDoc->maxScroll;
  191.         curScroll = theDoc->curScroll;
  192.         docExtent = theDoc->docExtent;
  193.  
  194.         while (theControl)    /* draw each scroll bar and thumb at proper value */
  195.         {
  196.             ref = GetCRefCon (theControl);
  197.             if ( ref == kVScrollTag )
  198.             {
  199.                 if (curScroll.v < maxScroll.v)
  200.                     value = ( (long)curScroll.v * (long)kControlMax) / (long)maxScroll.v;
  201.                 else
  202.                     value = kControlMax;
  203.                 HiliteControl (theControl, (docExtent.v > frameSize.v) ? 0 : 255);
  204.                 SetCtlValue (theControl, value);
  205.             }    
  206.             else if ( ref == kHScrollTag )
  207.             {
  208.                 if (curScroll.h < maxScroll.h)
  209.                     value = ( (long)curScroll.h * (long)kControlMax) / (long)maxScroll.h;
  210.                 else
  211.                     value = kControlMax;
  212.                 HiliteControl (theControl, (docExtent.h > frameSize.h) ? 0 : 255);
  213.                 SetCtlValue (theControl, value);
  214.             }
  215.             else
  216.                 HiliteControl (theControl, 0);
  217.                 
  218.             ShowControl (theControl);
  219.             theControl = (*theControl)->nextControl;
  220.         }
  221.     }
  222.     else    /* draw unhilighted scroll bars */
  223.     {
  224.         while (theControl)
  225.         {
  226.             HiliteControl (theControl, 255);
  227.             ShowControl (theControl);
  228.             theControl = (*theControl)->nextControl;
  229.         }
  230.     }
  231.  
  232.     DrawControls (theDoc);
  233.  
  234. } /* drawScrollBars */
  235.  
  236. /* ------------------------------------------------------------------------------------
  237.     moveScrollBars -    redraw the scroll bars after a window resizes
  238.     4.24.90kwgm
  239. ---------------------------------------------------------------------------------------*/
  240. static void 
  241. moveScrollBars (theWindow)
  242.     register WindowPtr        theWindow;
  243. {
  244.     long                    ref;
  245.     ControlHandle            controlList;
  246.     Rect                    portRect, sbRect;
  247.     
  248.     setPortClip (theWindow);
  249.     
  250.     controlList = ((WindowPeek)theWindow)->controlList;
  251.     portRect = theWindow->portRect;
  252.     
  253.     while (controlList)        /* move each control */
  254.     {
  255.         HideControl (controlList);
  256.         ref = GetCRefCon (controlList);
  257.         if ( ref == kVScrollTag)        /* vertical control */
  258.         {
  259.             vScrollBarRect (theWindow, &sbRect);
  260.             MoveControl (controlList, portRect.right - (kScrollBarSize - 1), portRect.top - 1);
  261.             SizeControl (controlList, kScrollBarSize, sbRect.bottom - sbRect.top);
  262.         }
  263.         else if (ref == kHScrollTag)    /* horizontal control */
  264.         {
  265.             hScrollBarRect (theWindow, &sbRect);
  266.             MoveControl (controlList, portRect.left - 1, portRect.bottom - (kScrollBarSize - 1));
  267.             SizeControl (controlList, sbRect.right - sbRect.left, kScrollBarSize);
  268.         }
  269.             
  270.         controlList = (*controlList)->nextControl;
  271.     }
  272.  
  273. } /* moveScrollBars */
  274.  
  275. /* --------------------------------------------------------------------------
  276.     hScrollBarRect    -    calculate the horizontal scroll bar rect
  277.     4.24.90kwgm
  278. ----------------------------------------------------------------------------- */
  279. Rect *
  280. hScrollBarRect (theWindow, theRect)
  281.     WindowPtr            theWindow;
  282.     Rect                *theRect;
  283. {
  284.     *theRect = theWindow->portRect;
  285.     
  286.     theRect->left -= 1;
  287.     theRect->top = theRect->bottom - (kScrollBarSize - 1);
  288.     theRect->right -= (kScrollBarSize - 2);
  289.     theRect->bottom += 1;
  290.     
  291.     return (theRect);
  292.     
  293. } /* hScrollBarRect */
  294.  
  295. /* --------------------------------------------------------------------------
  296.     vScrollBarRect    -    calculate the vertical scroll bar rect
  297.     4.24.90kwgm
  298. ----------------------------------------------------------------------------- */
  299. Rect *
  300. vScrollBarRect (theWindow, theRect)
  301.     WindowPtr            theWindow;
  302.     register Rect        *theRect;
  303. {
  304.     *theRect = theWindow->portRect;
  305.  
  306.     theRect->left = theRect->right - (kScrollBarSize - 1);
  307.     theRect->top -= 1;
  308.     theRect->right += 1;
  309.     theRect->bottom -= (kScrollBarSize - 2);
  310.  
  311.     return (theRect);
  312.     
  313. } /* vScrollBarRect */
  314.  
  315. /* --------------------------------------------------------------------------
  316.     invalScrollBars    -    invalidate the scrollbars and grow box area
  317.     4.24.90kwgm            erase the area if erase is true
  318. ----------------------------------------------------------------------------- */
  319. void
  320. invalScrollBars (theWindow, erase)
  321.     WindowPtr            theWindow;
  322.     Boolean                erase;
  323. {
  324.     Rect                scrollbarRect;
  325.     
  326.     hScrollBarRect (theWindow, &scrollbarRect);
  327.     InsetRect (&scrollbarRect, -1, -1);
  328.     
  329.     if (erase)
  330.         EraseRect (&scrollbarRect);
  331.  
  332.     InvalRect (&scrollbarRect);
  333.     
  334.     vScrollBarRect (theWindow, &scrollbarRect);
  335.     scrollbarRect.bottom += kScrollBarSize;
  336.     InsetRect (&scrollbarRect, -1, -1);
  337.  
  338.     if (erase)
  339.         EraseRect (&scrollbarRect);
  340.  
  341.     InvalRect (&scrollbarRect);
  342.             
  343. } /* invalScrollBars */
  344.  
  345. /*---------------------------------------------------------------------------
  346.     getWinRect -    returns window portRect in global coordinates
  347.     3.30.90kwgm        used for centering the window
  348. ----------------------------------------------------------------------------- */
  349. Rect *
  350. getWinRect ( theWindow, globalRect)
  351.     WindowPtr        theWindow;
  352.     Rect            *globalRect;
  353. {
  354.     GrafPtr        savePort;
  355.     
  356.     GetPort(&savePort);
  357.     SetPort(theWindow);
  358.     
  359.     *globalRect = theWindow->portRect;
  360.     
  361.     LocalToGlobal(&(topLeft(*globalRect)));
  362.     LocalToGlobal(&(botRight(*globalRect)));
  363.     
  364.     SetPort(savePort);
  365.     
  366.     return (globalRect);
  367.     
  368. } /* getWinRect */
  369.  
  370. /* ------------------------------------------------------------------------
  371.     setPortClip    -    set the clipping rectangle to the port rect
  372.     6/1/90kwgm
  373. --------------------------------------------------------------------------- */
  374. static void
  375. setPortClip (theWin)
  376.     WindowPtr        theWin;
  377. {
  378.     ClipRect (&theWin->portRect);
  379. }
  380.  
  381. /* --------------------------------------------------------------------------------
  382.     makeFrameRect -    create the window frame rectangle, ie, the content - scroll bar
  383.                     areas
  384. ----------------------------------------------------------------------------------- */
  385. Rect *
  386. makeFrameRect (theWindow, frameRectPtr)
  387.     WindowPtr    theWindow;
  388.     Rect        *frameRectPtr;
  389. {
  390.     Rect        localRect;
  391.     
  392.     localRect = theWindow->portRect;        /* port rect */
  393.     
  394.     localRect.right -= kScrollBarSize;        /* minus the scroll bars */
  395.     localRect.bottom -= kScrollBarSize;
  396.     
  397.     if (frameRectPtr)
  398.         *frameRectPtr = localRect;
  399.  
  400.     return (frameRectPtr);
  401.     
  402. } /* makeFrameRect */
  403.  
  404. /* --------------------------------------------------------------------------
  405.     setFrameClip -    set the port's clip rect to the content region of the window
  406.                     minus the scroll bar area
  407. ----------------------------------------------------------------------------- */
  408. void
  409. setFrameClip (theWindow, frameRectPtr)
  410.     WindowPtr        theWindow;
  411.     Rect            *frameRectPtr;
  412. {
  413.     Rect            frameRect;
  414.     
  415.     ClipRect (makeFrameRect (theWindow, &frameRect));
  416.     
  417.     if (frameRectPtr)
  418.         *frameRectPtr = frameRect;
  419.     
  420. } /* setFrameClip */
  421.  
  422. /* ----------------------------------------------------------------------------
  423.     centerWindow -    calculate the left, top position of a centered window
  424.     3.30.90kwgm        and move it to there
  425. ------------------------------------------------------------------------------- */
  426. void
  427. centerWindow (theWindow)
  428.     WindowPtr        theWindow;
  429. {
  430.     short            scrLength, scrHeight;
  431.     Rect            globalRect;
  432.     Point            where;
  433.  
  434.     /* get the window rect in global coordinates */
  435.     getWinRect (theWindow, &globalRect);
  436.     
  437.     scrLength = screenBits.bounds.right - screenBits.bounds.left;
  438.     scrHeight = (screenBits.bounds.bottom - screenBits.bounds.top) - GetMBarHeight();
  439.  
  440.     /* get the screen size */
  441.     where.v = (scrHeight - (globalRect.bottom - globalRect.top)) >> 1;
  442.     where.h = (scrLength - (globalRect.right - globalRect.left)) >> 1;
  443.     
  444.     MoveWindow (theWindow, where.h, where.v, false);
  445.     
  446. } /* centerWindow */
  447.     
  448. /* ===============================  EOF  =======================================
  449.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  450. ================================================================================ */
  451.